//
//  PhotoViewController.m
//  BlueToothProject
//
//  Created by Apple on 2017/3/7.
//  Copyright © 2017年 yxw. All rights reserved.
//

#import "PhotoViewController.h"
#import "PhotoCollectionViewCell.h"
#define MAIN_SCREEN_WIDTH  CGRectGetWidth([UIScreen mainScreen].bounds)
#define MAIN_SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)
@interface PhotoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@end
static NSString *cellID = @"cellID";
static CGFloat itemWidth = 80.f;

@implementation PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择图片";
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:cellID];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(popVc)];
    
}

- (void)popVc{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 19;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"a%zd",indexPath.row + 1] ofType:@"bmp"];
    cell.imageView.image = [UIImage imageWithContentsOfFile:imagePath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"a%zd",indexPath.row + 1] ofType:@"bmp"];
    if (imagePath) {
        self.sendBlock([UIImage imageWithContentsOfFile:imagePath]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - lazy loading
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(20, 5, 5, 5);
        layout.itemSize = CGSizeMake(itemWidth, itemWidth);
        layout.minimumLineSpacing = 20;
        layout.minimumInteritemSpacing = (MAIN_SCREEN_WIDTH - (4 * itemWidth))/5;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }return _collectionView;
}

@end
